﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
using JiraConnector.Model;
using ProjectJiraAddIn;

namespace JiraConnector
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }


        private void btnLoad3_Click(object sender, EventArgs e)
        {
            loadIssuesByJQL(this.txtJQL.Text);
        }


        /// <summary>
        /// Load provided JIRA URL to the Text Box
        /// </summary>
        /// <param name="jql"></param>
        void loadIssuesByJQL(String jql)
        {
            try
            {

                string username = Properties.Settings.Default.JIRA_Username; // this.txtUsername.Text;
                string password = Properties.Settings.Default.JIRA_Password; // this.txtPassword.Text;
                string url = Properties.Settings.Default.JIRA_URL + jql;

                showInTextBox("Load from URL: " + url);

                JIRAIssueList issues = JIRAConnector.getJIRAIssues(username, password, url);
                foreach (JIRAIssue i in issues.issues)
                {
                    showInTextBox("Issue: " + i.key + " " + i.fields["summary"]);
                }

                showInTextBox("JSON:" + issues.json);


                loadIssuesIntoDatagrid(issues);


                //Save the last JQL 
                Properties.Settings.Default.Last_JQL = this.txtJQL.Text;
                Properties.Settings.Default.Save();

               
            }
            catch (Exception ex)
            {
                showError(ex);
            }
        }

        private void showInTextBox(string text)
        {
            this.txtResult.Text = this.txtResult.Text + text + Environment.NewLine;
        }

        /// <summary>
        /// Load the issues, demonstrate mapping from JSON to Dategrid Row using Linq
        /// </summary>
        /// <param name="issues"></param>
        void loadIssuesIntoDatagrid(JIRAIssueList issues)
        {
            try
            {
                //load to datagrid 
                foreach (JIRAIssue i in issues.issues)
                {
                    DataSetJIRAIssues.JIRAIssuesRow r = this.dataSetJIRAIssues.JIRAIssues.NewJIRAIssuesRow();
                    r.Id = i.id;
                    r.Key = i.key;
                    r.Summary = i.fields["summary"].ToString();

                    if ( i.dueDate()!=null) 
                        r.DueDate = i.dueDate().ToString();

                    if (i.startDate() != null)
                        r.StartDate = i.startDate().ToString();

                    if (i.timeestimateHrs()!=null)
                        r.TimeEstimate = i.timeestimateHrs();
                    r.Reporter = i.reporter();
                    r.Assignee = i.assignee(); 
                    r.IssueType = i.issuetype();
                    r.AggregateTimeEstimate = i.aggregatetimeestimate();
                    r.AggregateTimeSpent = i.aggregatetimespent();

                    this.dataSetJIRAIssues.JIRAIssues.AddJIRAIssuesRow(row: r);
                }

                //optionally save as XML File for Excel 
                this.dataSetJIRAIssues.WriteXml("C:\\jira_tmp.xml");
            }
            catch (Exception ex)
            {
                showError(ex);
            }
        }

        /// <summary>
        /// Show error in the tool tip status 
        /// </summary>
        /// <param name="ex"></param>
        void showError(Exception ex)
        {
            this.toolStripStatusLabel1.Text = ex.Message;
        }

        /// <summary>
        /// Load Settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Load(object sender, EventArgs e)
        {
            try
            {
                this.txtJQL.Text = Properties.Settings.Default.Last_JQL;
            }
            catch (Exception ex)
            {
                showError(ex);
            }
        }


        /// <summary>
        /// Open the Settings Dialog 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Settings s = new Settings();
            s.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBoxProjectBridge about = new AboutBoxProjectBridge();
            about.ShowDialog();
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //open URL User Guide 
            String url = "https://code.google.com/p/project-bridge/wiki/User_Guide";
            System.Diagnostics.Process.Start(url);
        }
    }
}
